<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/uploadify/jquery.uploadify.min.js" ></script>
<link rel="stylesheet" charset="utf-8" type="text/css" href="__PUBLIC__/admin/js/uploadify/uploadify.css"> 
<script type="text/javascript" charset="utf-8" src="__PUBLIC__/admin/js/upload.js" ></script>
<!-- 配置文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="__PUBLIC__/admin/js/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
	var ue = UE.getEditor('edit');
</script>

<style>
.bg{ background: none; border: none;}
.uploadify-button-text{ display: inline-flex; }
.uploadify-button-text button{ margin: -10px; }
.upviewer img{ height:90px; width: 120px; }
.upviewer ul{ margin:0px; padding:0px; }
.upviewer li{ margin:0px; padding:0px; position: relative; display:inline-block; width:120px; height:90px; background-position: center; background-size: contain; background-repeat: no-repeat; float: left; margin-right:10px; border:1px #ccc solid; background-color:#f0f0f0;}
.upviewer li a{ display:inline-block; position: absolute; bottom:0px; left:0px; text-align:center; width:100%; background-color:rgba(0,0,0,0.3); color:red; cursor: pointer; }
.upviewer li:hover a{ background-color:rgba(0,0,0,0.8); }
</style>
<div class="container" style="padding-top: 50px;">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<form class="form-horizontal" role="form" id="post_from" action="{:U('add')}">
				<div class="form-group">
					<label for="title" class="col-sm-2 control-label">标题:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="title" name="title" />
					</div>
				</div>
				<div class="form-group">
					<label for="inputPassword3" class="col-sm-2 control-label">图片:</label>
					<div class="col-sm-10">
						<input type="file" class="form-control" id="cover" name="cover" placeholder="图片"><br />    	  
				    	<div id="view_list" name="cover" class="upviewer" ></div>
					</div>
				</div>
				<div class="form-group">
					<label for="number" class="col-sm-2 control-label">人数:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="number" name="number" />
					</div>
				</div>
				<div class="form-group">
					<label for="url" class="col-sm-2 control-label">直播地址:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="url" name="url" />
					</div>
				</div>
				<div class="form-group">
					<label for="sort" class="col-sm-2 control-label">排序:</label>
					<div class="col-sm-2">
						<input type="text" class="form-control" id="sort" name="sort" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">状态:</label>
					<div class="col-sm-10">
						<label for="is_status1" class="control-label">
							<input type="radio" id="is_status1" name="status" value="0">未直播
						</label>
						<label for="is_status2" class="control-label">
							<input type="radio" id="is_status2" name="status" value="1" checked>直播中
						</label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">是否显示:</label>
					<div class="col-sm-10">
						<label for="is_show1" class="control-label">
							<input type="radio" id="is_show1" name="show" value="0">否
						</label>
						<label for="is_show2" class="control-label">
							<input type="radio" id="is_show2" name="show" value="1" checked>是
						</label>
					</div>
				</div>
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						 <button type="button" class="btn btn-primary button">提交</button>
					</div>
				</div>
			</form>
		</div>
</div>
<script>
$(function(){
	initDelFile();
		
	initUploadify({ ID:'cover',url:"{:U('Upload/upload')}",success:function(obj){ 
			if(obj.s){
				$('#view_list').append('<li><img src="'+obj.d+'"/><a d_path="'+obj.d+'">删除</a></li>');
				initDelFile();
			}else{
				alert(obj.m);
			}
		} 
	});
		
	function initDelFile(){
		$('.upviewer li a').unbind('click').click(function(){
			if(!confirm('你确认删除吗?')) return false;
			var _this = $(this);
			var dpath = $(_this).attr('d_path');
			if(!dpath) return false;
			$.post("{:U('Upload/delfile')}",{filepath:dpath},function(obj){ 
				if(obj.s){ 
					$(_this).parents('li').remove();
				}else{
					alert(obj.m);
					$(_this).parents('li').remove();
				} 
			},'json');
		});
	}
		
	$(".button").click(function(){
		var $from = $("#post_from");
		var $data = {};
	 	var $from_data = $from.serializeArray();
		$.each($from_data, function(k, v){
			$data[v['name']] = v['value'];
		})
	 	$(".upviewer").each(function(){
			var upviewer_val = [];
			$(this).find("img").each(function(){
				upviewer_val.push($(this).attr('src'));
			});
			$data[$(this).attr("name")] = upviewer_val;
	 	});
		$.post($from.attr('action'), $data, function(obj){
			alert(obj.m);
			if(obj.s){
				if(typeof(obj.url)!= undefined && obj.url){
					location.href = obj.url;
				}
			}
		})
	})
});
</script>
